package com.ibp.service.impl;




import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.ibp.entity.HostMgr;
import com.ibp.service.DictDataService;
import com.ibp.service.HostMgrService;
import com.ibp.service.FSHttpService;
import com.ibp.utils.PropertyUtils;
import com.ibp.utils.StringUtil;

@Service("hostMgrServiceImpl")
public class HostMgrServiceImpl implements HostMgrService, InitializingBean {

	private static Logger logger = LoggerFactory
			.getLogger(HostMgrServiceImpl.class);

	@Autowired
	PropertyUtils propertyUtils;

	@Autowired
	FSHttpService fSHttpServiceImpl;

	private String ibms_ip = null;
	private String ibms_port = null;
	private String url = null;
	private String add_host=null;
	private String delete_host=null;
	  @Autowired
	  DictDataService dictDataServiceImpl;
	  private ServletContext servletContext; 

	


	@Override
	public void afterPropertiesSet() throws Exception {
		this.servletContext = dictDataServiceImpl.getServletContext();
	  	logger.info("初始化服务节点ip和port配置,servletContext:"+servletContext); 
	    Map<String,Map<String,String>> dictMap = (Map<String, Map<String, String>>) this.servletContext.getAttribute("dictMap");
	    
	    this.ibms_ip = 	  dictMap.get("http.ibms").get("ip");
	    this.ibms_port =  dictMap.get("http.ibms").get("port");
	    this.url = ("http://" + this.ibms_ip + ":" + this.ibms_port + "/");
	    this.add_host= dictMap.get("http.ibms").get("add.host");
	    this.delete_host= dictMap.get("http.ibms").get("delete.host");
	}

	// 删除host
	@Override
	public String deleteHost(HostMgr hostMgr) {
		
		String paramsJson = objToJson(hostMgr);
		logger.debug("删除记录的主键json：{}", paramsJson);
		String response = this.fSHttpServiceImpl.sendPostRequest(this.url
				+ this.delete_host, paramsJson);
		if (!StringUtil.isEmpty(response)) {
			logger.debug("删除成功");
		}
		return response;

	}

	

	// 增加host
	@Override
	public String addHost(HostMgr hostMgr) {
		String paramsJson = objToJson(hostMgr);
		logger.debug("增加记录json：{}", paramsJson);
		String response = this.fSHttpServiceImpl.sendPostRequest(this.url
				+ this.add_host, paramsJson);
		if (!StringUtil.isEmpty(response)) {
			logger.debug("增加成功");
		}
		return response; 
	}	

	public String objToJson(Object obj) {
		return JSON.toJSONString(obj);

	}

	public static List<HostMgr> getHostList(String hosts,String node){
		List<HostMgr> hostList = new ArrayList<HostMgr>();
		String[] hostArr = hosts.split(" ");
		for( int i = 0;i<hostArr.length;i++){
			HostMgr host = new HostMgr();
			host.setNode(node);
			host.setIp(hostArr[i].substring(0,hostArr[i].indexOf(":")));
			host.setPort(hostArr[i].substring(hostArr[i].indexOf(":")+1));
			hostList.add(host);
		}
		return hostList;
	}

}
